﻿#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <list>
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_CommHdr.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_Types.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_ProtocolHdr.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_AvCoder/NetEngine_AVPacket/AVPacket_Define.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_AvCoder/NetEngine_AVPacket/AVPacket_Error.h"

//g++ -std=gnu++11 -Wall -g TestFor_AVPacket.cpp -o TestFor_AVPacket -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_AvCoder -lNetEngine_BaseLib -lNetEngine_AVPacket -Wl,-rpath=./:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_AvCoder,--disable-new-dtags

void __stdcall AVPacket_Pack_CBNotify(XNETHANDLE xhNet, int nCvtType, int nCvtFrame, double dlTime, LPVOID lParam)
{
    printf("AVPacket_UNPack_CBNotify:%d %d %lf\n", nCvtType, nCvtFrame, dlTime);
}

int AVPacket_Test_FileConvert()
{
    XNETHANDLE xhAVFile = 0;
    double nTotalAVTime = 0;
    LPCTSTR lpszSrcFile = "480p.flv";
    LPCTSTR lpszDstFile = "480p.mp4";

    if (!AVPacket_FileConvert_Init(&xhAVFile, AVPacket_Pack_CBNotify))
    {
        printf("AVPacket_FileConvert_Init:%lX\n", AVPacket_GetLastError());
        return -1;
    }
    if (!AVPacket_FileConvert_Input(xhAVFile, lpszSrcFile, &nTotalAVTime))
    {
        printf("AVPacket_FileUNPack_Input:%lX\n", AVPacket_GetLastError());
        return -1;
    }
    printf("AVTime:%lf\n", nTotalAVTime);
    if (!AVPacket_FileConvert_Output(xhAVFile, lpszDstFile))
    {
        printf("AVPacket_FileUNPack_Output:%lX\n", AVPacket_GetLastError());
        return -1;
    }
    if (!AVPacket_FileConvert_Start(xhAVFile))
    {
        printf("AVPacket_FileUNPack_Start:%lX\n", AVPacket_GetLastError());
        return -1;
    }
    while (1)
    {
        BOOL bIsRun = FALSE;
        if (AVPacket_FileConvert_GetStatus(xhAVFile, &bIsRun))
        {
            if (!bIsRun)
            {
                break;
            }
        }
        Sleep(1000);
    }
    return AVPacket_FileConvert_Stop(xhAVFile);
}

int AVPacket_Test_FilePacket()
{
    XNETHANDLE xhAVFile = 0;
    LPCTSTR lpszVideoFile = "480p.264";
    LPCTSTR lpszAudioFile = "test.aac";
    LPCTSTR lpszDstFile = "480p.flv";

    if (!AVPacket_FilePacket_Init(&xhAVFile, AVPacket_Pack_CBNotify))
    {
        printf("AVPacket_FileUNPack_Init:%lX\n", AVPacket_GetLastError());
        return -1;
    }
    if (!AVPacket_FilePacket_Input(xhAVFile, lpszVideoFile, lpszAudioFile))
    {
        printf("AVPacket_FileUNPack_Input:%lX\n", AVPacket_GetLastError());
        return -1;
    }
    if (!AVPacket_FilePacket_Output(xhAVFile, lpszDstFile))
    {
        printf("AVPacket_FileUNPack_Output:%lX\n", AVPacket_GetLastError());
        return -1;
    }
    if (!AVPacket_FilePacket_Start(xhAVFile))
    {
        printf("AVPacket_FileUNPack_Start:%lX\n", AVPacket_GetLastError());
        return -1;
    }

    while (1)
    {
        BOOL bIsRun = FALSE;
        if (AVPacket_FilePacket_GetStatus(xhAVFile, &bIsRun))
        {
            if (!bIsRun)
            {
                break;
            }
        }
        Sleep(1000);
    }
    return AVPacket_FilePacket_Stop(xhAVFile);
}

int AVPacket_Test_UNPacket()
{
    XNETHANDLE xhAVFile = 0;
    BOOL bAudio = FALSE;
    BOOL bVideo = FALSE;

    LPCTSTR lpszVideoFile = "480p.264";
    LPCTSTR lpszAudioFile = "test.aac";
    LPCTSTR lpszSrcFile = "480p.flv";

    if (!AVPacket_FileUNPack_Init(&xhAVFile, AVPacket_Pack_CBNotify))
    {
        printf("AVPacket_FileUNPack_Init:%lX\n", AVPacket_GetLastError());
        return -1;
    }
    double dlAVTime = 0;
    if (!AVPacket_FileUNPack_Input(xhAVFile, lpszSrcFile, &bVideo, &bAudio, &dlAVTime))
    {
        printf("AVPacket_FileUNPack_Input:%lX\n", AVPacket_GetLastError());
        return -1;
    }
    printf("AVTime:%lf\n", dlAVTime);
    if (!AVPacket_FileUNPack_Output(xhAVFile, lpszVideoFile,lpszAudioFile))
    {
        printf("AVPacket_FileUNPack_Output:%lX\n", AVPacket_GetLastError());
        return -1;
    }
    if (!AVPacket_FileUNPack_Start(xhAVFile))
    {
        printf("AVPacket_FileUNPack_Start:%lX\n", AVPacket_GetLastError());
        return -1;
    }
    while (1)
    {
        BOOL bIsRun = FALSE;
        if (AVPacket_FileUNPack_GetStatus(xhAVFile, &bIsRun))
        {
            if (!bIsRun)
            {
                break;
            }
        }
        Sleep(1000);
    }
    return AVPacket_FilePacket_Stop(xhAVFile);
}
int main()
{
    //AVPacket_Test_FileConvert();
    AVPacket_Test_UNPacket();
    //AVPacket_Test_FilePacket();
    return 1;
}
